﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using TS_XY_Train_Repaire.Materials;


namespace TS_XY_Train_Repaire.Main_Base
{
    public partial class Sys_Setting_Station_WorkPlace : DevExpress.XtraEditors.XtraForm
    {
        public Sys_Setting_Station_WorkPlace()
        {
            InitializeComponent();
        }
        LeverString_SysOrganization _ls = new LeverString_SysOrganization();
        private void Sys_Setting_Station_WorkPlace_Load(object sender, EventArgs e)
        {
            BreeList();
            InitFunctionGrid();
            TreeList_dep.ExpandAll();
        }


        public void BreeList() {
            TreeList_dep.Nodes.Clear();
            DataTable dt = new DataTable();
            TreeList_dep.Refresh();
            TreeList_dep.ExpandAll();
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            ArrayList pList = new ArrayList();
            _ls = new LeverString_SysOrganization();
            _ls.f_id = "M-1";
            _ls.f_name = "系统底层数据目录";
            pList.Add(_ls);
            try {
                string strSQL = "select * from TS_S_B_Station_WorkPlace where f_del=0 and f_deep=0";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL,conn);
                SqlDataReader read = cmd.ExecuteReader();
                string _strUPID = "and (";
                int i = 0;
                while(read.Read()){
                    _ls = new LeverString_SysOrganization();
                    _ls.f_id = read["f_id"].ToString();
                    _ls.f_name=read["f_name"].ToString();
                    _ls.f_rbid = read["f_rbid"].ToString();
                    _ls.f_rsid = read["f_rsid"].ToString();
                    _ls.f_type = read["f_type"].ToString();
                    _ls.f_stack = read["f_stack"].ToString();
                    _ls.f_stacknum = read["f_stacknum"].ToString();
                    _ls.f_level = read["f_level"].ToString();
                    _ls.f_deep = read["f_deep"].ToString();
                    _ls.f_parent = "M-1";
                    if (i == 0)
                    {
                        _strUPID += "(f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    else
                    {
                        _strUPID += "or (f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    pList.Add(_ls);
                    i++;

                }
                 _strUPID += ")";
                 if (_strUPID.Length > 7)
                 {
                     read.Close();
                     strSQL = "select * from TS_S_B_Station_WorkPlace where f_deep>0 and f_del=0 " + _strUPID + " order by f_deep,f_level,f_name asc";
                     cmd = new SqlCommand(strSQL, conn);
                     read = cmd.ExecuteReader();
                     string _UPID = "";
                     while (read.Read())
                     {
                         _ls = new LeverString_SysOrganization();
                         _ls.f_id = read["f_id"].ToString();
                         _ls.f_name = read["f_name"].ToString();
                         _ls.f_rbid = read["f_rbid"].ToString();
                         _ls.f_rsid = read["f_rsid"].ToString();
                         _ls.f_type = read["f_type"].ToString();
                         _ls.f_stack = read["f_stack"].ToString();
                         _ls.f_stacknum = read["f_stacknum"].ToString();
                         _ls.f_level = read["f_level"].ToString();
                         _ls.f_deep = read["f_deep"].ToString();
                         _UPID = read["f_level"].ToString();
                         _UPID = _UPID.Substring(0, _UPID.Length - 1);
                         _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                         _ls.f_parent = _UPID;
                         pList.Add(_ls);
                     }
                     read.Close();
                     conn.Close();
                     conn.Dispose();
                 }
            }
            catch { 
            }
            TreeList_dep.DataSource = pList;
          }
        public void InitFunctionGrid()
        { 
          DataTable dt = new DataTable();
          TS_Win_Class.FilledDataTable("select w.f_id,w.f_name,r.f_name f_rbid,s.f_name f_rsid,w.f_stack,w.f_stacknum,case w.f_type  when 0 then '机车' when 1 then '配件' when 2 then '设备' when 3 then '混合' when 4 then '虚拟库房' end as f_type  from TS_S_B_Station_WorkPlace w left join TS_S_T_P_RailwayBureau r on w.f_rbid=r.f_id left join  TS_S_T_P_RailwayStations s on w.f_rsid=s.f_id  where  w.f_del=0", dt);
            gc_contion.DataSource = dt;  
 
        }

        #region  //添加节点数据工位//
        private void barAddNode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Sys_Setting_Station_WorkPlace_comprehensive barAddNodes = new Sys_Setting_Station_WorkPlace_comprehensive();
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            _ls.f_id = TreeList_dep.FocusedNode["f_id"].ToString();
            _ls.f_name = TreeList_dep.FocusedNode["f_name"].ToString();
            _ls.f_rbid = TreeList_dep.FocusedNode["f_rbid"].ToString();
            _ls.f_rsid = TreeList_dep.FocusedNode["f_rsid"].ToString();
            _ls.f_type = TreeList_dep.FocusedNode["f_type"].ToString();
            _ls.f_stack = TreeList_dep.FocusedNode["f_stack"].ToString();
            _ls.f_stacknum = TreeList_dep.FocusedNode["f_stacknum"].ToString();
            _ls.f_level = TreeList_dep.FocusedNode["f_level"].ToString();
            _ls.f_deep = TreeList_dep.FocusedNode["f_deep"].ToString();
            barAddNodes.LodeStr = "添加节点";
            barAddNodes._lt = _ls;
            barAddNodes.Lode = "barAddNodes";
            if (barAddNodes.ShowDialog()==DialogResult.OK)
            {
                BreeList();
                InitFunctionGrid();
                TreeList_dep.SetFocusedNode(TreeList_dep.FindNodeByFieldValue("f_id", _ls.f_id));
                TreeList_dep.ExpandAll();
                TreeList_dep.Focus();
           }
            
        }
        //删除节点数据工位//
        private void barDelteNodes_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (TreeList_dep.FocusedNode["f_id"].ToString() == "M-1")
            {
                MessageBox.Show("此节点数据不允许删除！！！");
             }else{
                if(MessageBox.Show("确定要删除此条数据吗？","提示",MessageBoxButtons.YesNo)==DialogResult.Yes){
                    string strSql = "update TS_S_B_Station_WorkPlace set f_del=1,f_workerid=" + 116+ "  where f_id=" + TreeList_dep.FocusedNode["f_id"].ToString();
                 TS_Win_Class.MySqlCom(strSql);
                 string Strsql = "update TS_S_B_Station_WorkPlace set f_del=1,f_workerid=" + 116 + " where f_level  like '%," + TreeList_dep.FocusedNode["f_id"].ToString() + ",%' and f_del=0";
                 TS_Win_Class.MySqlCom(Strsql);
                 MessageBox.Show("删除成功","提示");
                 BreeList();
                 InitFunctionGrid();
                 TreeList_dep.SetFocusedNode(TreeList_dep.FindNodeByFieldValue("f_id", _ls.f_id));
                 TreeList_dep.ExpandAll();
                }
             }
        }
        //编译节点数据工位//
        private void barEditNode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Sys_Setting_Station_WorkPlace_comprehensive barEditNodes = new Sys_Setting_Station_WorkPlace_comprehensive();
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            _ls.f_id = TreeList_dep.FocusedNode["f_id"].ToString();
            _ls.f_name = TreeList_dep.FocusedNode["f_name"].ToString();
            _ls.f_rbid = TreeList_dep.FocusedNode["f_rbid"].ToString();
            _ls.f_rsid = TreeList_dep.FocusedNode["f_rsid"].ToString();
            _ls.f_type = TreeList_dep.FocusedNode["f_type"].ToString();
            _ls.f_stack = TreeList_dep.FocusedNode["f_stack"].ToString();
            _ls.f_stacknum = TreeList_dep.FocusedNode["f_stacknum"].ToString();
            _ls.f_level = TreeList_dep.FocusedNode["f_level"].ToString();
            _ls.f_deep = TreeList_dep.FocusedNode["f_deep"].ToString();
            barEditNodes._lt = _ls;
            barEditNodes.LodeStr = "修改节点";
            barEditNodes.Lode = "barEditNodes";
            if (barEditNodes.ShowDialog() == DialogResult.OK)
            {
                BreeList();
                InitFunctionGrid();
                TreeList_dep.SetFocusedNode(TreeList_dep.FindNodeByFieldValue("f_id", _ls.f_id));
                TreeList_dep.ExpandAll();

            }
        }
        #endregion

        private void Refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            barauthorization.Enabled = false;
            BreeList();
            InitFunctionGrid();
            TreeList_dep.ExpandAll();
        }
        //加载数据
        private void TreeList_dep_DoubleClick(object sender, EventArgs e)
        {
            barauthorization.Enabled = true;
            if (TreeList_dep.FocusedNode["f_id"].ToString().Substring(0, 1) == "M")
            {
              
            }
            else
            {
                string sql = "select f_id,f_name,f_stack,f_stacknum,(select f_name from TS_S_T_P_RailwayBureau where TS_S_T_P_RailwayBureau.f_id=TS_S_B_Station_WorkPlace.f_rbid)as f_rbid ,(select f_name from TS_S_T_P_RailwayStations where TS_S_T_P_RailwayStations.f_id=TS_S_B_Station_WorkPlace.f_rsid)as f_rsid,case f_type  when 0 then '机车' when 1 then '配件' when 2 then '设备' when 3 then '混合' when 4 then '虚拟库房' end as f_type from TS_S_B_Station_WorkPlace where f_id=" + TreeList_dep.FocusedNode["f_id"].ToString();
                DataTable dt = new DataTable();
                TS_Win_Class.FilledDataTable(sql, dt);
                gc_contion.DataSource = dt;

            }
            
        }

        #region  //摄像头授权管理//
        private void barauthorization_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.Main_Base.Sys_Setting_Station_WorkPlace_Camera Camera = new Sys_Setting_Station_WorkPlace_Camera();
            if (this.gc_contion.Name == "gc_contion")
            {
                Camera.tree_id = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, "f_id").ToString();
                Camera.tree_name = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, "f_name").ToString();
                if (Camera.ShowDialog() == DialogResult.OK)
                {
                    
                }
            }
            else {
                if (gc_contion.Name == "gc_contion")
                {
                  Camera.tree_id = TreeList_dep.FocusedNode["f_id"].ToString();
                 Camera.tree_name = TreeList_dep.FocusedNode["f_name"].ToString();
                if (Camera.ShowDialog() == DialogResult.OK)
                {

                }
                 }
               
            }
        
           
          
        }

        private void gc_contion_Click(object sender, EventArgs e)
        {
            barauthorization.Enabled = true;
            barAddNode.Enabled = false;
            barDelteNodes.Enabled = false;
            barEditNode.Enabled = false;
        }
        #endregion

        private void barAdmin_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
             Sys_Setting_Station_Camera Camera = new Sys_Setting_Station_Camera();
            if(Camera.ShowDialog()==DialogResult.OK){
            
            }
        
        }

        //private void TreeList_dep_MouseClick(object sender, MouseEventArgs e)
        //{   
        //    barauthorization.Enabled = true;
        //    barAddNode.Enabled = true;
        //    barDelteNodes.Enabled = true;
        //    barEditNode.Enabled = true;           
        //    DataTable dt = new DataTable();
        //    string ConStrsql = " select w.f_id,w.f_name,r.f_name f_rbid,s.f_name f_rsid,w.f_stack,w.f_stacknum,case w.f_type  when 0 then '机车' when 1 then '配件' when 2 then '设备' when 3 then '混合' when 4 then '虚拟库房' end as f_type  from TS_S_B_Station_WorkPlace w left join TS_S_T_P_RailwayBureau r on w.f_rbid=r.f_id left join  TS_S_T_P_RailwayStations s on w.f_rsid=s.f_id   where w.f_level like '%," + TreeList_dep.FocusedNode["f_id"].ToString() + ",%' and w.f_del=0";
        //    TS_Win_Class.FilledDataTable(ConStrsql, dt);
        //    gc_contion.DataSource = dt;
        //}

        private void Sys_Setting_Station_WorkPlace_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);

        }

        private void TreeList_dep_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                if (TreeList_dep.FocusedNode["f_id"].ToString() == "M-1")
                {
                    barDelteNodes.Enabled = false;
                    barEditNode.Enabled = false;
                }
                else
                {
                    barAddNode.Enabled = true;
                    barauthorization.Enabled = true;
                    barDelteNodes.Enabled = true;
                    barEditNode.Enabled = true;
                    DataTable dt = new DataTable();
                    string ConStrsql = " select w.f_id,w.f_name,r.f_name f_rbid,s.f_name f_rsid,w.f_stack,w.f_stacknum,case w.f_type  when 0 then '机车' when 1 then '配件' when 2 then '设备' when 3 then '混合' when 4 then '虚拟库房' end as f_type  from TS_S_B_Station_WorkPlace w left join TS_S_T_P_RailwayBureau r on w.f_rbid=r.f_id left join  TS_S_T_P_RailwayStations s on w.f_rsid=s.f_id   where w.f_level like '%," + TreeList_dep.FocusedNode["f_id"].ToString() + ",%' and w.f_del=0";
                    TS_Win_Class.FilledDataTable(ConStrsql, dt);
                    gc_contion.DataSource = dt;
                }
            }
            catch { 
            
            }
        }

       

    }
}
